package krow.dev.weathertest;

import java.io.*;
import java.net.*;
import java.util.*;

import javax.xml.parsers.*;

import krow.dev.weathertest.model.*;

import org.w3c.dom.*;

import com.example.weathertest.*;
import com.example.weathertest.R.*;

import android.app.*;
import android.content.*;
import android.content.DialogInterface.OnCancelListener;
import android.net.*;
import android.os.*;
import android.support.v4.view.*;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.*;
import android.view.*;
import android.widget.*;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {
	ArrayList<WeatherData> arItem;
	ShowThread mThread;
	CodeChange cd = new CodeChange();
	//ProgressDialog mProgress; 
	public MyProgressDialog mProgress;
	
	private final int COUNT = 3; // 페이지 
	private int mPrevPosition; // 페이지 뷰를 위한 변수 ( 이전 포지션 )
	private int mNextPosition;
	int mNowPosition; // 지금 페이지가 몇번인지 확인하기 위한 변수
	
	int x1, x2; // 앱 종료전 페이지 추가가 몇번까지 됬는지 알아오기 위한 변수
	boolean threadWorkEnd = false;
	
	PagerAdap mAdapter;
	private LinearLayout mPageMark;
	
	String locate, locate2, locate3;
	String mlocateName, mlocateName2, mlocateName3;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
		setContentView(R.layout.layout_pager);
		mPageMark = (LinearLayout)findViewById(R.id.page_mark);
		
		
		ViewPager vP = (ViewPager)findViewById(R.id.pager);
		mAdapter = new PagerAdap(vP, getApplicationContext());
		vP.setAdapter(mAdapter);
		vP.setCurrentItem(0);
		
		// 프레퍼런스에 저장해둔 정보 가져와서 다시 보여주기
		SharedPreferences AddItemPref = getSharedPreferences("AddItem", 0); // AddItem 프레퍼런스 가져옴
		x1 = AddItemPref.getInt("AddP1", 0); 
		x2 = AddItemPref.getInt("AddP2", 0);
		System.out.println(x1 + " " + x2);
		if( x1 == 1 && x2 == 0) { // 1페이지 추가한 프레퍼런스값이 있고, 2페이지 값이 null이면
			System.out.println("1페이지 추가 프레퍼 저장된거 불러와씀");
			mAdapter.addItem(1, 1); // 뷰페이저아답터에 1페이지를 추가함
			addPageMark();
			
		}
		else if(x1 ==1 && x2 == 2) { // 프레퍼런스값에 1페이지, 2페이지 값이 존재(1과 2로 지정해놨음) 이 있으면
			mAdapter.addItem(1, 1);  // 뷰페이저에 1페이지
			addPageMark();
			System.out.println("2페이지 추가 프레퍼 저장된거 불러와씀");
			mAdapter.addItem(2, 2); // 뷰페이저에 2페이지 추가함
			addPageMark();
		}
		
		// 메인용
		SharedPreferences pref = getSharedPreferences("Pref", 0);
		locate = pref.getString("locateC", null);
		mlocateName = pref.getString("locateN", "검색 정보가 없습니다.");
		
		// 2페이지용 - 테스트 중
		SharedPreferences pref2 = getSharedPreferences("Pref2", 0);
		locate2 = pref2.getString("locateC2", null);
		mlocateName2 = pref2.getString("locateN2", "검색 정보가 없습니다.");
		
		// 3페이지용 - 테스트 중
		SharedPreferences pref3 = getSharedPreferences("Pref3", 0);
		locate3 = pref3.getString("locateC3", null);
		mlocateName3 = pref3.getString("locateN3", "검색 정보가 없습니다.");
		
		// 
		mPrevPosition = 0;
		addPageMark();		// 첫 메인페이지를 가리키는 뷰페이저 인디케이터 생성
		mPageMark.getChildAt(mPrevPosition).setBackgroundResource(R.drawable.page_select);
		//
		

		getActionBar().setHomeButtonEnabled(true);
		
		vP.setCurrentItem(0); // 시작 시 뷰페이저의 첫화면을 메인페이지로 만듬
		
		mProgress = MyProgressDialog.show(MainActivity.this, "업데이트 중..", null, true, false, new DialogInterface.OnCancelListener() {
			
			@Override
			public void onCancel(DialogInterface dialog) {
				// TODO Auto-generated method stub
				mThread.mQuit = true;
				mProgress.dismiss();
			}
		});
		//mProgress.dismiss();
		//mProgress = new ProgressDialog(MainActivity.this);
		//mProgress.setProgressStyle(R.style.MyDialog);
		//mProgress.setTitle("업데이트");
		//mProgress.setMessage("업데이트 중입니다..");
		//mProgress.setCancelable(true);
		//mProgress.addContentView(new ProgressBar(this),new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		/*mProgress.setButton("취소", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				mThread.mQuit = true;
				mProgress.dismiss();
			}
		});*/
		//mProgress.show();
		
		// 메인 페이지 자동 새로고침 및 처음 실행했을 때를 위한 코드
		if(locate == null) {
			System.out.println("locate값이 null임");
			Intent intent = new Intent(MainActivity.this, SubActivity.class);
			startActivityForResult(intent, 1);
			mNowPosition = 0;
		}
		else if(locate != null) {
			System.out.println("앱 실행되자마자 받은 locate - " + locate);
			mThread = new ShowThread("http://weather.yahooapis.com/forecastrss?w=" + locate + "&u=c", mlocateName, 0);
			
			mThread.start();
			mNowPosition = 0;
		}
		
		
		
		vP.setOnPageChangeListener(new OnPageChangeListener() {	// 뷰페이저서의 아이템, 즉 페이지가 넘어갈때마다 작동하는 리스너
			//아이템이 선택이 되었으면
			@Override public void onPageSelected(int position) {
				mPageMark.getChildAt(mPrevPosition).setBackgroundResource(R.drawable.page_not);	//이전 페이지에 해당하는 페이지 표시 이미지 변경
				mPageMark.getChildAt(position).setBackgroundResource(R.drawable.page_select);		//현재 페이지에 해당하는 페이지 표시 이미지 변경
				//mPageMark.getChildAt(mNextPosition).setBackgroundResource(R.drawable.page_not); // 다음페이지에 해당하는 페이지 표시 이미지 변경
				mPrevPosition = position;				//이전 포지션 값을 현재로 변경
				
				/*
				mProgress = new ProgressDialog(MainActivity.this);
				mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				mProgress.setTitle("업데이트");
				mProgress.setMessage("업데이트 중입니다..");
				mProgress.setCancelable(false);
				mProgress.setButton("취소", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						mThread.mQuit = true;
						mProgress.dismiss();
					}
				});
				mProgress.show();*/
				
				mProgress = MyProgressDialog.show(MainActivity.this, "업데이트 중..", null, true, false, new DialogInterface.OnCancelListener() {
					
					@Override
					public void onCancel(DialogInterface dialog) {
						// TODO Auto-generated method stub
						mThread.mQuit = true;
						mProgress.dismiss();
					}
				});
				
				if(position == 0) { // 메인페이지 일때
					SharedPreferences pref = getSharedPreferences("Pref", 0);
					locate = pref.getString("locateC", null);
					mlocateName = pref.getString("locateN", "검색 정보가 없습니다.");
					
					System.out.println("메인페이지 선택됬음");
					System.out.println(mlocateName);
					
					
					if(locate == null) {
						System.out.println("locate값이 null임");
						Intent intent = new Intent(MainActivity.this, SubActivity.class);
						startActivityForResult(intent, 1);
						mNowPosition = 0;
					}
					else if(locate != null) {
						System.out.println(locate);
						
						
						mThread.interrupt();
						try{
							
							mThread = new ShowThread("http://weather.yahooapis.com/forecastrss?w=" + locate +  "&u=c", mlocateName, position);
							
							//mThread.sleep(1000);
							
							mThread.start();
							
							mNowPosition = 0;
							
						}
						catch(Exception e) {}
						
					}
					System.out.println("총 페이지 갯수 - "+  mAdapter.getCount());
					
				}
				else if(position == 1) { // 1페이지 일때
					
					SharedPreferences pref2 = getSharedPreferences("Pref2", 0);
					locate2 = pref2.getString("locateC2", null);
					mlocateName2 = pref2.getString("locateN2", "검색 정보가 없습니다.");
					
					System.out.println("1페이지 선택됬음");
					System.out.println(mlocateName2);
					
					if(locate2 == null) {
						System.out.println("locate2값이 null임");
						Intent intent = new Intent(MainActivity.this, SubActivity.class);
						startActivityForResult(intent, 2);
						mNowPosition = 1;
					}
					else if(locate2 != null) {
						System.out.println(locate2);
						mThread.interrupt();
						try{
							mThread = new ShowThread("http://weather.yahooapis.com/forecastrss?w=" + locate2 +  "&u=c", mlocateName2, position);
							
							//mThread.sleep(1000);
							
							mThread.start();
							
							mNowPosition = 1;
							
						}
						catch(Exception e) {}
					}
					System.out.println("총 페이지 갯수 - "+  mAdapter.getCount());
					
				}
				else if(position == 2) { // 2페이지 일때
					SharedPreferences pref3 = getSharedPreferences("Pref3", 0);
					locate3 = pref3.getString("locateC3", null);
					mlocateName3 = pref3.getString("locateN3", "검색 정보가 없습니다.");
					
					System.out.println("2페이지 일때");
					System.out.println(mlocateName3);
					
					if(locate3 == null) {
						System.out.println("locate3값이 null임");
						Intent intent = new Intent(MainActivity.this, SubActivity.class);
						startActivityForResult(intent, 3);
						mNowPosition = 2;
					}
					else if(locate3 != null) {
						System.out.println(locate3);
						mThread.interrupt();
						try{
							mThread = new ShowThread("http://weather.yahooapis.com/forecastrss?w=" + locate3 +  "&u=c", mlocateName3, position);
							
							//mThread.sleep(1000);
							
							mThread.start();
							
							mNowPosition = 2;
							
						}
						catch(Exception e) {}
					}
					System.out.println("총 페이지 갯수 - "+  mAdapter.getCount());
				}
					
				
				
			}
			@Override public void onPageScrolled(int position, float positionOffest, int positionOffsetPixels) {}
			@Override public void onPageScrollStateChanged(int state) {}
		});
		
	
			
		
	}
	private void addPageMark(){ // 뷰페이저 인디케이터 그리기 ( 페이지 추가될때마다 같이 추가되는 형식 )
		ImageView iv = new ImageView(getApplicationContext());	    
		iv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		iv.setBackgroundResource(R.drawable.page_not);
		mPageMark.addView(iv);
		
	} 
	private void removePageMark(int index) { // 뷰페이저 인디케이터 지우기 ( 페이지 제거될때마다 같이 제거되는 형식 )
		
		mPageMark.removeViewAt(index);
	}
	
	
	public class PagerAdap extends PagerAdapter{ // 뷰페이저 아답터 
		private Context mContext;
		private ViewPager mPager;
		//public LayoutInflater mInflater;
		
		private ArrayList<Integer> mItems;
		
		public PagerAdap(ViewPager vp, Context con) { 
			super(); 
			mPager = vp;
			mContext = con;
			//mInflater = LayoutInflater.from(con);
			
			mItems = new ArrayList<Integer>();		
			mItems.add(ViewPagerAdd.TYPE_HomeView);
			
		}

		
		@Override 
		public int getCount() { 
			//return 3; 
			return mItems == null ? 0:mItems.size();
		}

		
		@Override 
		public Object instantiateItem(View pager, int position) {
			
			View v = new ViewPagerAdd().getView(mItems.get(position), mContext);
			((ViewPager)pager).addView(v, 0);
			return v;
		}

		
		@Override public void destroyItem(View pager, int position, Object view) {
			((ViewPager)pager).removeView((View)view);
		}

	
		@Override public boolean isViewFromObject(View view, Object obj) { return view == obj; }

		@Override public void finishUpdate(View arg0) {}
		@Override public void restoreState(Parcelable arg0, ClassLoader arg1) {}
		@Override public Parcelable saveState() { return null; }
		@Override public void startUpdate(View arg0) {}
		
		public void addItem(int type, int position){
			if(position == 1) {
				mItems.add(type, position);
				mNowPosition = 1;
				notifyDataSetChanged();
				
				System.out.println("addItem" + type + position);
				//mPager.setCurrentItem(1, true); 
				
			}
			else if(position == 2) {
				mItems.add(type, position);
				mNowPosition = 2;
				notifyDataSetChanged();
				
				System.out.println("addItem" + type + position);
				//mPager.setCurrentItem(2, true);
			}
				
		}
		
		public void removeItem(int type, int position){
			if(position == 1) {
				
				if(mAdapter.getCount() == 2){ // 페이지 갯수가 2개일때
					
					mItems.remove(1);
					mPrevPosition = position - 1;
					Log.d("즐겨찾기 제거 메소드", "페이지 갯수 2개일때, 1페이지 제거됨");
					notifyDataSetChanged();
					
					SharedPreferences pref2 = getSharedPreferences("AddItem", 0);
					SharedPreferences.Editor edit2 = pref2.edit();
					
					edit2.putInt("AddP1", 0);
					edit2.putInt("AddP2", 0);
					edit2.commit();
					
					mPager.setCurrentItem(0, true);
				}
				else if(mAdapter.getCount() == 3) { // 페이지 갯수가 3개일때
					
					mItems.remove(1);
					notifyDataSetChanged();
					
					mPrevPosition = position - 1;   
					Log.d("즐겨찾기 제거 메소드", "페이지 갯수 3개일때, 1페이지 제거됨");
					
					
					SharedPreferences pref2 = getSharedPreferences("AddItem", 0);
					SharedPreferences.Editor edit2 = pref2.edit();
					
					edit2.putInt("AddP1", 1);
					edit2.putInt("AddP2", 0);
					edit2.commit();
					
					SharedPreferences locatePref = getSharedPreferences("Pref3", 0);
					String pref3C = locatePref.getString("locateC3", null);
					String pref3N = locatePref.getString("locateN3", "검색 정보가 없습니다.");
					Log.d("즐겨찾기 제거 메소드", pref3C);
					Log.d("즐겨찾기 제거 메소드", pref3N);
					
					SharedPreferences locatePrefChg = getSharedPreferences("Pref2", 0);
					SharedPreferences.Editor edit = locatePrefChg.edit();
					
					edit.putString("locateC2", pref3C);
					edit.putString("locateN2", pref3N);
					edit.commit();
					
					mPager.setCurrentItem(0, true);
				}
				
			}
			else if (position == 2) {
				
				mItems.remove(2);
				mPrevPosition = position - 1;
				System.out.println("2페이지 제거");
				
				notifyDataSetChanged();
				mPager.setCurrentItem(1, true);
				
				SharedPreferences pref2 = getSharedPreferences("AddItem", 0);
				SharedPreferences.Editor edit2 = pref2.edit();
				
				
				edit2.putInt("AddP1", 1);
				edit2.putInt("AddP2", 0);
				edit2.commit();
			}
					
		}
		
		public void setItem(int position) {
			if (position == 0) {
				notifyDataSetChanged();
				mPager.setCurrentItem(0, true);
			}
			else if(position == 1) {
				notifyDataSetChanged();
				mPager.setCurrentItem(1, true);
			}
			else if(position == 2) {
				
				notifyDataSetChanged();
				mPager.setCurrentItem(2, true);
				
			}
			else if(position == 4) {
				
				notifyDataSetChanged();
				mPager.setCurrentItem(2, true);
				
			}
		}
		
		
		
	}

	
	// 인텐트 데이터랑 결과 코드 받아 스레드를 실행할 메소드
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		//TextView locateNameView = (TextView)findViewById(R.id.locate);
		
		
		if(resultCode==RESULT_OK) {// 액티비티가 정상적으로 종료되었을 경우
			if(requestCode==1) { // Main에서 호출한 경우에만 처리합니다.
				
				//프레퍼런스에 검색한 정보 저장해두기 - 메인 페이지용
				SharedPreferences pref = getSharedPreferences("Pref", 0);
				SharedPreferences.Editor edit = pref.edit();
				
				String locateCode = data.getStringExtra("data_ID");
				String locateName = data.getStringExtra("data_LName"); 
				edit.putString("locateC", locateCode);
				edit.putString("locateN", locateName);
				edit.commit();
				//
				
				//locateNameView.setText("검색 지역 : " + data.getStringExtra("data_LName"));
				//getActionBar().setTitle(data.getStringExtra("data_LName"));
				mThread = new ShowThread("http://weather.yahooapis.com/forecastrss?w=" + data.getStringExtra("data_ID") + "&u=c", locateName, 0);
				
				mThread.start();
				
			}
			else if(requestCode==2) { // 검색버튼 눌러서 올 1번째 페이지용 - 테스트
				
				
				System.out.println("요청코드2번받고 들어옴, 프레퍼런스2 기능 되나 확인 바람");
				SharedPreferences pref2 = getSharedPreferences("Pref2", 0);
				SharedPreferences.Editor edit2 = pref2.edit();
				
				String locateCode2 = data.getStringExtra("data_ID");
				String locateName2 = data.getStringExtra("data_LName"); 
				edit2.putString("locateC2", locateCode2);
				edit2.putString("locateN2", locateName2);
				edit2.commit();
				
				mThread = new ShowThread("http://weather.yahooapis.com/forecastrss?w=" + data.getStringExtra("data_ID") + "&u=c", locateName2, 1);
				
				mThread.start();
				
			}
			else if(requestCode==3) { // 검색버튼 눌러서 올 2번째 페이지용 - 테스트
				System.out.println("요청코드3번받고 들어옴, 프레퍼런스3 기능 되나 확인 바람");
				SharedPreferences pref3 = getSharedPreferences("Pref3", 0);
				SharedPreferences.Editor edit3 = pref3.edit();
				
				String locateCode3 = data.getStringExtra("data_ID");
				String locateName3 = data.getStringExtra("data_LName"); 
				edit3.putString("locateC3", locateCode3);
				edit3.putString("locateN3", locateName3);
				edit3.commit();
				
				mThread = new ShowThread("http://weather.yahooapis.com/forecastrss?w=" + data.getStringExtra("data_ID") + "&u=c", locateName3, 2);
				
				mThread.start();	
			}
			else if(requestCode==4) { // 즐겨찾기 1번째 페이지용 - 테스트
				mAdapter.addItem(1, 1);					
				addPageMark();
				
				System.out.println("요청코드4번받고 들어옴, 프레퍼런스2 기능 되나 확인 바람");
				SharedPreferences pref2 = getSharedPreferences("Pref2", 0);
				SharedPreferences.Editor edit2 = pref2.edit();
				
				String locateCode2 = data.getStringExtra("data_ID");
				String locateName2 = data.getStringExtra("data_LName"); 
				edit2.putString("locateC2", locateCode2);
				edit2.putString("locateN2", locateName2);
				edit2.commit();
				
				mThread = new ShowThread("http://weather.yahooapis.com/forecastrss?w=" + data.getStringExtra("data_ID") + "&u=c", locateName2, 1);
				
				mThread.start();
				
			}
			
			else if(requestCode==5) { // 즐겨찾기 2번째 페이지용 - 테스트
				mAdapter.addItem(2, 2);					
				addPageMark();
				
				System.out.println("요청코드5번받고 들어옴, 프레퍼런스3 기능 되나 확인 바람");
				SharedPreferences pref3 = getSharedPreferences("Pref3", 0);
				SharedPreferences.Editor edit3 = pref3.edit();
				
				String locateCode3 = data.getStringExtra("data_ID");
				String locateName3 = data.getStringExtra("data_LName"); 
				edit3.putString("locateC3", locateCode3);
				edit3.putString("locateN3", locateName3);
				edit3.commit();
				
				mThread = new ShowThread("http://weather.yahooapis.com/forecastrss?w=" + data.getStringExtra("data_ID") + "&u=c", locateName3, 2);
				
				mThread.start();	
			}
			
			else if(requestCode==6) { // 즐겨찾기 2번째 페이지용 - 테스트
				mAdapter.addItem(2, 2);					
				addPageMark();
				
				System.out.println("요청코드6번받고 들어옴, 프레퍼런스3 기능 되나 확인 바람");
				SharedPreferences pref3 = getSharedPreferences("Pref3", 0);
				SharedPreferences.Editor edit3 = pref3.edit();
				
				String locateCode3 = data.getStringExtra("data_ID");
				String locateName3 = data.getStringExtra("data_LName"); 
				edit3.putString("locateC3", locateCode3);
				edit3.putString("locateN3", locateName3);
				edit3.commit();
				Log.d("요청코드", locateCode3 + locateName3);
				
				mThread = new ShowThread("http://weather.yahooapis.com/forecastrss?w=" + data.getStringExtra("data_ID") + "&u=c", locateName3, 4);
				
				mThread.start();	
			}
		}
	}//*/

	
	
	 
	// 지역코드 받아서 해당 지역의 xml데이터를 받아 파싱까지 할 작업 스레드
	class ShowThread extends Thread {
		
		
		String dataInfoXmlAddr;
		String dataInfoXml;
		boolean mQuit;
		String dataLocate;
		String textList;
		String dataCode; // 오늘 현재 날씨 코드 저장용 변수
		String dataCodeC; // 오늘 현재 날씨 코드를 한글로 전환해서 저장할 변수
		String dataTemp; // 오늘 현재 날씨 온도 저장용 변수
		int tPostion;
		
		int dataCodeInt;
		
		String data1stDay, data2ndDay, data3rdDay, data4thDay, data5thDay;
		String data1stDayC, data2ndDayC, data3rdDayC, data4thDayC, data5thDayC;
		String data1stLow, data2ndLow, data3rdLow, data4thLow, data5thLow;
		String data1stHigh, data2ndHigh, data3rdHigh, data4thHigh, data5thHigh;
		String data1stCode, data2ndCode, data3rdCode, data4thCode, data5thCode, data1stCodeC;
		
		ShowThread(String id, String locate, int position) {
			mQuit = false;
			
			dataInfoXmlAddr = id;
			dataInfoXml = "";
			textList = "";
			dataCode = "";
			
			dataCodeC = "";
			dataTemp = "";
			dataLocate = locate;
			
			tPostion = position;
			
			data1stDay = ""; 
			data1stDayC = "";
			data1stLow = "";
			data1stHigh = "";
			data1stCode = "";
			data1stCodeC = ""; // 메인날씨 변환 코드 저장용 변수
			
			dataCodeInt = 3200;
		}
		
		public void run() {
			//Toast.makeText(Main.this, "메인액티비티의 스레드 진입", Toast.LENGTH_SHORT).show();
			System.out.println("작업용 백그라운드 스레드 가동");
			StringBuilder WeatherXml = new StringBuilder();
			
			try{
				sleep(300);
				URL url = new URL(dataInfoXmlAddr);
				HttpURLConnection conn = (HttpURLConnection) url.openConnection();
				if (conn != null) {
					conn.setConnectTimeout(10000);
					conn.setUseCaches(false);
					if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
						BufferedReader br = new BufferedReader(
								new InputStreamReader(conn.getInputStream(), "UTF-8"));
						for (;;) {
							String line = br.readLine();
							if (line == null) {
								break;
							}
							WeatherXml.append(line + '\n');
						}
						br.close();
						dataInfoXml = WeatherXml.toString();
					}
					conn.disconnect();
				}
				
				///*/
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				DocumentBuilder builder = factory.newDocumentBuilder();
				InputStream istream = new ByteArrayInputStream(dataInfoXml.getBytes("utf-8"));
				Document doc = builder.parse(istream);
				
				Element findItemNode = doc.getDocumentElement();
				
				// 오늘 날씨의 현재 온도를 파싱하기 위한 코드
				NodeList todayWeathers = findItemNode.getElementsByTagName("yweather:condition");
				Node item = todayWeathers.item(0); // 오늘 현재 날씨에 대한 노드를 리스트에서 0번이니 추출해옴
				NamedNodeMap Attrs = item.getAttributes();
				
				Node attrCodeNow = Attrs.item(1);
				Node attrTemp = Attrs.item(2);
				dataCode = attrCodeNow.getNodeValue();
				dataCodeInt = Integer.parseInt(dataCode);
				
				dataCodeC = cd.CodeChange(dataCode);
				
				dataTemp = attrTemp.getNodeValue();
				//
				
				NodeList allWeathers = findItemNode.getElementsByTagName("yweather:forecast");
				
				for(int i = 0; i< allWeathers.getLength(); i++) {
					Node itemWeathers = allWeathers.item(i);
					
					NamedNodeMap WeatherAttrs = itemWeathers.getAttributes();
					Node attrDay = WeatherAttrs.item(0);
					Node attrLow = WeatherAttrs.item(2);
					Node attrHigh = WeatherAttrs.item(3);
					Node attrCode = WeatherAttrs.item(5);
					if(i == 0) {
						data1stDay = attrDay.getNodeValue();
						data1stDayC = cd.DayChange(data1stDay);
						data1stLow = attrLow.getNodeValue();
						data1stHigh = attrHigh.getNodeValue();
						//data1stCode = attrCode.getNodeValue();
						//data1stCodeC = CodeChange(data1stCode);
					}
					else if(i == 1) {
						data2ndDay = attrDay.getNodeValue();
						data2ndDayC = cd.DayChange(data2ndDay);
						data2ndLow = attrLow.getNodeValue();
						data2ndHigh = attrHigh.getNodeValue();
						data2ndCode = attrCode.getNodeValue();
					}
					else if(i == 2) {
						data3rdDay = attrDay.getNodeValue();
						data3rdDayC = cd.DayChange(data3rdDay);
						data3rdLow = attrLow.getNodeValue();
						data3rdHigh = attrHigh.getNodeValue();
						data3rdCode = attrCode.getNodeValue();
					}
					else if(i == 3) {
						data4thDay = attrDay.getNodeValue();
						data4thDayC = cd.DayChange(data4thDay);
						data4thLow = attrLow.getNodeValue();
						data4thHigh = attrHigh.getNodeValue();
						data4thCode = attrCode.getNodeValue();
					}
					else if(i == 4) {
						data5thDay = attrDay.getNodeValue();
						data5thDayC = cd.DayChange(data5thDay);
						data5thLow = attrLow.getNodeValue();
						data5thHigh = attrHigh.getNodeValue();
						data5thCode = attrCode.getNodeValue();
					}
				}
			}
			catch(Exception ex){}
			
			if(mQuit) {
				return;
			}
			
			mAfterShow.sendEmptyMessage(0);
			
		}
	}
	
	
	
	
	///*
	Handler mAfterShow = new Handler() {
		public void handleMessage(Message msg) {
			Log.e("핸들러", "들어오긴 하냐");
			mProgress.dismiss();
			
			TextView locate1;
			ImageView todayImage;
			ListView MyList;
			TextView todayText1;
			TextView todayText2;
			TextView todayText3;
			
			
			
			
			if(mThread.tPostion == 0) {
			
				locate1 = (TextView)findViewById(R.id.locate);
				todayImage = (ImageView)findViewById(R.id.todayimg);
				MyList = (ListView)findViewById(R.id.list);
				todayText1 = (TextView)findViewById(R.id.todaytext1);
				todayText2 = (TextView)findViewById(R.id.todaytext2);
				todayText3 = (TextView)findViewById(R.id.todaytext3);
				
				locate1.setText(mThread.dataLocate);
				
				System.out.println(mThread.dataCode);
				//int x = Integer.parseInt(mThread.dataCode);
				int x = mThread.dataCodeInt;
				
				if(x == 0 || x == 2) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_hurricane));
				}
				else if(x == 1) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_thundersun));
				}
				else if(x == 3 || x == 4 || x == 45 || x == 47 || x == 37) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_thunder));
				}
				else if(x == 5 || x == 6 || x == 10 || x == 35) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_hailalt));
				}
				else if(x == 7 || x == 8 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_snowflakes));
				}
				else if(x == 9 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_drizzle));
				}
				else if(x == 11 || x == 12 || x == 17 || x == 40 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_rainalt));
				}
				else if(x == 13 || x == 18 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_snowalt));
				}
				else if(x == 14 || x == 15 || x == 16 || x == 41 || x == 43 || x == 46 || x == 42) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_snow));
				}
				else if(x == 19 || x == 21 || x == 22 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_fogalt));
				}
				else if(x == 20) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_fog));
				}
				else if(x == 44) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_cloud));
				}
				else if(x == 23 || x == 24 || x == 25 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_wind));
				}
				
				else if(x == 26 || x == 38 || x == 39) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_whitecloud));
				}
				else if(x == 27 || x == 29) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_cloudmoon));
				}
				else if(x == 28 || x == 30 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_cloudsun));
				}
				else if(x == 31 || x == 32 || x == 33 || x == 34 || x == 36) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_sun));
				}
				else if(x == 3200) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_q));
				}
				
				todayText1.setText(mThread.dataTemp + " ˚C");
				todayText2.setText(mThread.dataCodeC);
				todayText3.setText(mThread.data1stDayC + '\n' + mThread.data1stLow + "˚C / " + mThread.data1stHigh + "˚C");
				
				
				
				arItem = new ArrayList<WeatherData>();
				WeatherData mi;
				mi = new WeatherData(mThread.data2ndDayC, mThread.data2ndLow, mThread.data2ndHigh, mThread.data2ndCode);arItem.add(mi);
				mi = new WeatherData(mThread.data3rdDayC, mThread.data3rdLow, mThread.data3rdHigh, mThread.data3rdCode);arItem.add(mi);
				mi = new WeatherData(mThread.data4thDayC, mThread.data4thLow, mThread.data4thHigh, mThread.data4thCode);arItem.add(mi);
				mi = new WeatherData(mThread.data5thDayC, mThread.data5thLow, mThread.data5thHigh, mThread.data5thCode);arItem.add(mi);
				
				
				MyListAdapter MyAdapter = new MyListAdapter(MainActivity.this, R.layout.listview_weatherlistview, arItem);
				
				
				MyList.setAdapter(MyAdapter);
				
				
				
			}
			else if(mThread.tPostion == 1) {
				
				SharedPreferences AddItemPref = getSharedPreferences("AddItem", 0);
				SharedPreferences.Editor edit = AddItemPref.edit();
				
				
				int saveItem = 1;
				edit.putInt("AddP1", saveItem);
				edit.commit();
				System.out.println("1페이지 아이템추가 프레퍼 저장됬음");
				
				
				todayImage = (ImageView)findViewById(R.id.todayimg2);
				MyList = (ListView)findViewById(R.id.list2);
				todayText1 = (TextView)findViewById(R.id.todaytext1_2);
				todayText2 = (TextView)findViewById(R.id.todaytext2_2);
				todayText3 = (TextView)findViewById(R.id.todaytext3_2);
				locate1 = (TextView)findViewById(R.id.locate2);
				locate1.setText(mThread.dataLocate);
				
				System.out.println(mThread.dataCode);
				int x = mThread.dataCodeInt;
				//int x = Integer.parseInt(mThread.dataCode);
				
				if(x == 0 || x == 2) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_hurricane));
				}
				else if(x == 1) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_thundersun));
				}
				else if(x == 3 || x == 4 || x == 45 || x == 47 || x == 37) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_thunder));
				}
				else if(x == 5 || x == 6 || x == 10 || x == 35) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_hailalt));
				}
				else if(x == 7 || x == 8 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_snowflakes));
				}
				else if(x == 9 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_drizzle));
				}
				else if(x == 11 || x == 12 || x == 17 || x == 40 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_rainalt));
				}
				else if(x == 13 || x == 18 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_snowalt));
				}
				else if(x == 14 || x == 15 || x == 16 || x == 41 || x == 43 || x == 46 || x == 42) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_snow));
				}
				else if(x == 19 || x == 21 || x == 22 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_fogalt));
				}
				else if(x == 20) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_fog));
				}
				else if(x == 44) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_cloud));
				}
				else if(x == 23 || x == 24 || x == 25 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_wind));
				}
				
				else if(x == 26 || x == 38 || x == 39) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_whitecloud));
				}
				else if(x == 27 || x == 29) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_cloudmoon));
				}
				else if(x == 28 || x == 30 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_cloudsun));
				}
				else if(x == 31 || x == 32 || x == 33 || x == 34 || x == 36) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_sun));
				}
				else if(x == 3200) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_q));
				}
				
				todayText1.setText(mThread.dataTemp + " ˚C");
				
				
				todayText2.setText(mThread.dataCodeC);
				todayText3.setText(mThread.data1stDayC + '\n' + mThread.data1stLow + "˚C / " + mThread.data1stHigh + "˚C");
				
				
				
				arItem = new ArrayList<WeatherData>();
				WeatherData mi;
				mi = new WeatherData(mThread.data2ndDayC, mThread.data2ndLow, mThread.data2ndHigh, mThread.data2ndCode);arItem.add(mi);
				mi = new WeatherData(mThread.data3rdDayC, mThread.data3rdLow, mThread.data3rdHigh, mThread.data3rdCode);arItem.add(mi);
				mi = new WeatherData(mThread.data4thDayC, mThread.data4thLow, mThread.data4thHigh, mThread.data4thCode);arItem.add(mi);
				mi = new WeatherData(mThread.data5thDayC, mThread.data5thLow, mThread.data5thHigh, mThread.data5thCode);arItem.add(mi);
				
				
				MyListAdapter MyAdapter = new MyListAdapter(MainActivity.this, R.layout.listview_weatherlistview, arItem);
				
				
				MyList.setAdapter(MyAdapter);
				mAdapter.setItem(mThread.tPostion);
				
				
			}
			else if(mThread.tPostion == 2) {
				
				SharedPreferences AddItemPref = getSharedPreferences("AddItem", 0);
				SharedPreferences.Editor edit = AddItemPref.edit();
				
				
				int saveItem = 2;
				edit.putInt("AddP2", saveItem);
				edit.commit();
				System.out.println("2페이지 아이템추가 프레퍼 저장됬음");
			
				todayImage = (ImageView)findViewById(R.id.todayimg3);
				MyList = (ListView)findViewById(R.id.list3);
				todayText1 = (TextView)findViewById(R.id.todaytext1_3);
				todayText2 = (TextView)findViewById(R.id.todaytext2_3);
				todayText3 = (TextView)findViewById(R.id.todaytext3_3);
				locate1 = (TextView)findViewById(R.id.locate3);
				locate1.setText(mThread.dataLocate);
				
				System.out.println(mThread.dataCode);
				int x = mThread.dataCodeInt;
				//int x = Integer.parseInt(mThread.dataCode);
				
				if(x == 0 || x == 2) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_hurricane));
				}
				else if(x == 1) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_thundersun));
				}
				else if(x == 3 || x == 4 || x == 45 || x == 47 || x == 37) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_thunder));
				}
				else if(x == 5 || x == 6 || x == 10 || x == 35) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_hailalt));
				}
				else if(x == 7 || x == 8 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_snowflakes));
				}
				else if(x == 9 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_drizzle));
				}
				else if(x == 11 || x == 12 || x == 17 || x == 40 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_rainalt));
				}
				else if(x == 13 || x == 18 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_snowalt));
				}
				else if(x == 14 || x == 15 || x == 16 || x == 41 || x == 43 || x == 46 || x == 42) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_snow));
				}
				else if(x == 19 || x == 21 || x == 22 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_fogalt));
				}
				else if(x == 20) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_fog));
				}
				else if(x == 44) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_cloud));
				}
				else if(x == 23 || x == 24 || x == 25 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_wind));
				}
				
				else if(x == 26 || x == 38 || x == 39) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_whitecloud));
				}
				else if(x == 27 || x == 29) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_cloudmoon));
				}
				else if(x == 28 || x == 30 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_cloudsun));
				}
				else if(x == 31 || x == 32 || x == 33 || x == 34 || x == 36) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_sun));
				}
				else if(x == 3200) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_q));
				}
				
				todayText1.setText(mThread.dataTemp + " ˚C");
				
				
				todayText2.setText(mThread.dataCodeC);
				todayText3.setText(mThread.data1stDayC + '\n' + mThread.data1stLow + "˚C / " + mThread.data1stHigh + "˚C");
				
				
				
				arItem = new ArrayList<WeatherData>();
				WeatherData mi;
				mi = new WeatherData(mThread.data2ndDayC, mThread.data2ndLow, mThread.data2ndHigh, mThread.data2ndCode);arItem.add(mi);
				mi = new WeatherData(mThread.data3rdDayC, mThread.data3rdLow, mThread.data3rdHigh, mThread.data3rdCode);arItem.add(mi);
				mi = new WeatherData(mThread.data4thDayC, mThread.data4thLow, mThread.data4thHigh, mThread.data4thCode);arItem.add(mi);
				mi = new WeatherData(mThread.data5thDayC, mThread.data5thLow, mThread.data5thHigh, mThread.data5thCode);arItem.add(mi);
				
				
				MyListAdapter MyAdapter = new MyListAdapter(MainActivity.this, R.layout.listview_weatherlistview, arItem); // 뷰페이저 내부 뷰 안에 넣을 리스트뷰에 접합시킬 아답터
				
				
				MyList.setAdapter(MyAdapter);
				mAdapter.setItem(mThread.tPostion);
				
				
			}
			// 테스트중 14.1.29. 20:59
			else if(mThread.tPostion == 4) {
				
				SharedPreferences AddItemPref = getSharedPreferences("AddItem", 0);
				SharedPreferences.Editor edit = AddItemPref.edit();
				
				
				int saveItem = 2;
				edit.putInt("AddP2", saveItem);
				edit.commit();
				System.out.println("4페이지 아이템추가 프레퍼 저장됬음");
			
				todayImage = (ImageView)findViewById(R.id.todayimg2);
				MyList = (ListView)findViewById(R.id.list2);
				todayText1 = (TextView)findViewById(R.id.todaytext1_2);
				todayText2 = (TextView)findViewById(R.id.todaytext2_2);
				todayText3 = (TextView)findViewById(R.id.todaytext3_2);
				locate1 = (TextView)findViewById(R.id.locate2);
				locate1.setText(mThread.dataLocate);
				
				System.out.println(mThread.dataCode);
				int x = mThread.dataCodeInt;
				//int x = Integer.parseInt(mThread.dataCode);
				
				if(x == 0 || x == 2) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_hurricane));
				}
				else if(x == 1) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_thundersun));
				}
				else if(x == 3 || x == 4 || x == 45 || x == 47 || x == 37) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_thunder));
				}
				else if(x == 5 || x == 6 || x == 10 || x == 35) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_hailalt));
				}
				else if(x == 7 || x == 8 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_snowflakes));
				}
				else if(x == 9 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_drizzle));
				}
				else if(x == 11 || x == 12 || x == 17 || x == 40 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_rainalt));
				}
				else if(x == 13 || x == 18 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_snowalt));
				}
				else if(x == 14 || x == 15 || x == 16 || x == 41 || x == 43 || x == 46 || x == 42) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_snow));
				}
				else if(x == 19 || x == 21 || x == 22 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_fogalt));
				}
				else if(x == 20) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_fog));
				}
				else if(x == 44) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_cloud));
				}
				else if(x == 23 || x == 24 || x == 25 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_wind));
				}
				
				else if(x == 26 || x == 38 || x == 39) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_whitecloud));
				}
				else if(x == 27 || x == 29) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_cloudmoon));
				}
				else if(x == 28 || x == 30 ) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_cloudsun));
				}
				else if(x == 31 || x == 32 || x == 33 || x == 34 || x == 36) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_sun));
				}
				else if(x == 3200) {
					todayImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_q));
				}
				
				todayText1.setText(mThread.dataTemp + " ˚C");
				
				
				todayText2.setText(mThread.dataCodeC);
				todayText3.setText(mThread.data1stDayC + '\n' + mThread.data1stLow + "˚C / " + mThread.data1stHigh + "˚C");
				
				
				
				arItem = new ArrayList<WeatherData>();
				WeatherData mi;
				mi = new WeatherData(mThread.data2ndDayC, mThread.data2ndLow, mThread.data2ndHigh, mThread.data2ndCode);arItem.add(mi);
				mi = new WeatherData(mThread.data3rdDayC, mThread.data3rdLow, mThread.data3rdHigh, mThread.data3rdCode);arItem.add(mi);
				mi = new WeatherData(mThread.data4thDayC, mThread.data4thLow, mThread.data4thHigh, mThread.data4thCode);arItem.add(mi);
				mi = new WeatherData(mThread.data5thDayC, mThread.data5thLow, mThread.data5thHigh, mThread.data5thCode);arItem.add(mi);
				
				
				MyListAdapter MyAdapter = new MyListAdapter(MainActivity.this, R.layout.listview_weatherlistview, arItem); // 뷰페이저 내부 뷰 안에 넣을 리스트뷰에 접합시킬 아답터
				
				
				MyList.setAdapter(MyAdapter);
				mAdapter.setItem(mThread.tPostion);
				
				
				
			}
			
			
		
		}
	};
	//*/
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.actionbar_main_menu, menu);

		return super.onCreateOptionsMenu(menu);
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		
		case R.id.remove:
		
			
			if(mNowPosition == 0) {
				Toast.makeText(getApplicationContext(), "메인 페이지는 삭제할 수 없습니다.", Toast.LENGTH_SHORT).show();
				break;
			}
			else if(mNowPosition == 1) {
				mAdapter.removeItem(1, 1);
				removePageMark(1);
				Log.d("Main_Action bar", "삭제버튼, 지금 보고 있는 페이지가 1페이지 일때");
				break;
			}
			else if(mNowPosition == 2) {
				mAdapter.removeItem(2, 2);
				removePageMark(2);
				Log.d("Main_Action bar", "삭제버튼, 지금 보고 있는 페이지가 2페이지 일때");
				break;
			}
			else {
				Toast.makeText(getApplicationContext(), "잘못된 입력입니다", Toast.LENGTH_SHORT).show();
				break;
			}
			
			
			
		
		case R.id.plus:
			
			if(mAdapter.getCount() <= 1) {		
				Intent intent = new Intent(MainActivity.this, SubActivity.class);
				startActivityForResult(intent, 4);
				
				Log.d("Main_Action bar", "추가버튼, 지금 페이지가 1개일때(메인포함)");
				
				break;
			}
			else if(mAdapter.getCount() <= 2) {
				if(mNowPosition == 0) {
					Intent intent = new Intent(MainActivity.this, SubActivity.class);
					startActivityForResult(intent, 6);
					
					Log.d("Main_Action bar", "추가버튼, 지금 페이지가 2개일때(메인포함), 보고 있는 페이지가 메인페일때");
					break;
				}
				else if(mNowPosition == 1) {
					Intent intent = new Intent(MainActivity.this, SubActivity.class);
					startActivityForResult(intent, 5);
					
					Log.d("Main_Action bar", "추가버튼, 지금 페이지가 2개일때(메인포함), 보고 있는 페이지가 1페일때");
					break;
				}
				
				
				
			}
			
			else {
				Toast.makeText(getApplicationContext(), "3개 넘으면 안됨니다 고갱님", Toast.LENGTH_SHORT).show();
				break;
			}
			
			
			
			
		case R.id.search:
			
			if(mNowPosition == 0){
				Intent intent = new Intent(MainActivity.this, SubActivity.class);
				Log.d("Main_Action bar", "서치버튼, 현재 보고 있는 페이지가 0일때");
				startActivityForResult(intent, 1);
				break;
			}
			else if(mNowPosition == 1) {
				Intent intent = new Intent(MainActivity.this, SubActivity.class);
				Log.d("Main_Action bar", "서치버튼, 현재 보고 있는 페이지가 1일때");
				startActivityForResult(intent, 2);
				break;
			}
			else if(mNowPosition == 2) {
				Intent intent = new Intent(MainActivity.this, SubActivity.class);
				Log.d("Main_Action bar", "서치버튼, 현재 보고 있는 페이지가 2일때");
				startActivityForResult(intent, 3);
				break;
			}
			
			
		
		
		case R.id.info:
			new AlertDialog.Builder(this)
    		.setTitle("프로그램 소개")
    		.setMessage("야후날씨 API를 통한 날씨정보 어플임.")
    		.setIcon(R.drawable.ic_launcher)
    		.setPositiveButton("닫기", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
				}
			})
    		.show();
			break;

		case R.id.end:
			finish();
			System.exit(0);
			break;
		
		}
		return super.onOptionsItemSelected(item);
		
	}

}
	

